package com.scriptlib;

import android.media.Image;
import android.util.Log;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
public class ScreenHelper {

    private Object screenCapturer;
    private Method captureMethod;
    private int width = -1;
    private int height = -1;
    private int rowStride = -1;
    private boolean isInit = false;
    private byte[] screenData;

    public byte[] GetScreenData() {
        return screenData;
    }

    public int GetWidth() {
        return width;
    }

    public int GetHeight() {
        return height;
    }

    public boolean GetLastFrame() {
        try {
            Image image = (Image) captureMethod.invoke(screenCapturer);
            if (image == null) {
                return false;
            }
            ByteBuffer byteBuf = image.getPlanes()[0].getBuffer();
            if (!isInit) {
                isInit = true;
                width = image.getWidth();
                height = image.getHeight();
                rowStride = image.getPlanes()[0].getRowStride();
                screenData = new byte[width * height * 4];
            }
            for (int i = 0; i < height; i++) {
                byteBuf.position(i * rowStride);
                byteBuf.get(screenData, i * width * 4, width * 4);
            }
            return true;
        } catch (InvocationTargetException | IllegalAccessException e) {
            Log.e("ScreenHelper", e.toString());
            return false;
        }
    }

    public ScreenHelper(Object obj) {
        try {
            Field field = obj.getClass().getDeclaredField("mScreenCapturer");
            field.setAccessible(true);
            screenCapturer = field.get(obj);
            captureMethod = field.getType().getDeclaredMethod("capture");
            captureMethod.setAccessible(true);
        } catch (NoSuchFieldException | IllegalAccessException | NoSuchMethodException e) {
            Log.e("ScreenHelper", e.toString());
        }
    }

//    public void TestCapture() {
//        try {
//            Image image = (Image) captureMethod.invoke(screenCapturer);
//            if (image == null) {
//                return;
//            }
//            Image.Plane plane = image.getPlanes()[0];
//            ByteBuffer buffer = plane.getBuffer();
//            buffer.position(0);
//            int pixelStride = plane.getPixelStride();
//            int rowPadding = plane.getRowStride() - pixelStride * image.getWidth();
//            Bitmap bitmap = Bitmap.createBitmap(image.getWidth() + rowPadding / pixelStride, image.getHeight(), Bitmap.Config.ARGB_8888);
//            bitmap.copyPixelsFromBuffer(buffer);
//
//            File f = new File("/sdcard/TestCapture.png");
//            FileOutputStream fOut = null;
//            try {
//                fOut = new FileOutputStream(f);
//            } catch (FileNotFoundException e) {
//                e.printStackTrace();
//            }
//            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
//            try {
//                fOut.flush();
//                fOut.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//            return;
//        } catch (InvocationTargetException | IllegalAccessException e) {
//            Log.e("ScreenHelper", e.toString());
//            return;
//        }
//    }
}
